package com.xxzx.publicprevention.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import com.xxzx.publicprevention.pojo.GeoZhyhd;
import com.xxzx.publicprevention.pojo.TbUploadfile;
import com.xxzx.publicprevention.pojo.TbYhdfilerelation;
import com.xxzx.publicprevention.service.GeoZhyhdService;
import com.xxzx.publicprevention.service.TbYhdfilerelationService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (GeoZhyhd)表控制层
 *
 * @author makejava
 * @since 2020-11-13 10:49:37
 */
@RestController
@RequestMapping("geoZhyhd")
public class GeoZhyhdController {
    /**
     * 服务对象
     */
    @Resource
    private GeoZhyhdService geoZhyhdService;

    @Resource
    private TbYhdfilerelationService tbYhdfilerelationService;
    @Resource
    private TbUploadfileController tbUploadfileController;


    @GetMapping("getInfoByid")
    public GeoZhyhd getInfoByid(Integer id) {
        try {
            return geoZhyhdService.queryById(id);
        } catch (Exception ee) {
            return null;
        }
    }

    @GetMapping("getZhyhdByKeywords")
    public List<GeoZhyhd> getZhyhdByKeywords(String keywords) {
        try {
            List<GeoZhyhd> geoZhyhd = geoZhyhdService.getZhyhdByKeywords(keywords);
            return geoZhyhd;
        } catch (Exception ee) {
            return null;
        }
    }

    @GetMapping("gis_Contains")
    public List<GeoZhyhd> gis_Contains(String coords) {
        try {
            return geoZhyhdService.gis_Contains(coords);
        } catch (Exception ee) {
            return null;
        }
    }

    @PostMapping("GetAll")
    public List<GeoZhyhd> GetAll(@RequestBody GeoZhyhd geoZhyhd) {
        try {
            return geoZhyhdService.queryAll(geoZhyhd);
        } catch (Exception ee) {
            return null;
        }
    }

    @PostMapping("insert")
    @ResponseBody
    public int insert(String yhd, String attaIds) {
        try {

            GeoZhyhd geoZhyhd = JSON.parseObject(yhd, GeoZhyhd.class);

            GeoZhyhd insert = geoZhyhdService.insert(geoZhyhd);
            // 关联附件
            addFile(attaIds, insert.getId());
            return insert.getId();
        } catch (Exception ee) {
            ee.getMessage();
            return -1;
        }
    }

    private void addFile(String attaIds, Integer id) {
        // 关联附件
        List<Integer> aIds = JSONArray.parseArray(attaIds, Integer.class);
        if (aIds != null) {
            TbYhdfilerelation rela;
            for (Integer aid : aIds) {
                rela = new TbYhdfilerelation();
                rela.setYhdid(id);
                rela.setFileid(aid);
                tbYhdfilerelationService.insert(rela);
            }
        }
    }

    private void deleteFile(Integer id) {
        List<TbYhdfilerelation> relationsByZhyhdid = tbYhdfilerelationService.getRelationsByZhyhdid(id);
        if (relationsByZhyhdid.size() > 0) {
            for (TbYhdfilerelation c : relationsByZhyhdid) {
                tbUploadfileController.deleteById(c.getFileid());
                tbYhdfilerelationService.deleteById(c.getId());
            }
        }
    }


    @PostMapping("update")
    @ResponseBody
    public int update(String yhd, String attaIds) {
        try {

            GeoZhyhd geoZhyhd = JSON.parseObject(yhd, GeoZhyhd.class);
            GeoZhyhd update = geoZhyhdService.update(geoZhyhd);
          /*  deleteFile(geoZhyhd.getId());*/


            List<Integer> aIds = JSONArray.parseArray(attaIds, Integer.class);

            List<TbYhdfilerelation> relationsByZhyhdid = tbYhdfilerelationService.getRelationsByZhyhdid(geoZhyhd.getId());

            if (relationsByZhyhdid.size() > 0) {
                for (TbYhdfilerelation c : relationsByZhyhdid) {

                    boolean flag = true;
                    if (aIds != null) {
                        for (Integer a : aIds) {
                            if (a.equals(c.getFileid())) {
                                flag = false;
                            }
                        }
                    }

                    if (flag) {
                        tbUploadfileController.deleteById(c.getFileid());
                    }

                    tbYhdfilerelationService.deleteById(c.getId());

                }
            }

            addFile(attaIds, geoZhyhd.getId());
            return update.getId();
        } catch (Exception ee) {
            ee.getMessage();
            return -1;
        }
    }

    @GetMapping("getZhyhdByNd")
    public List<GeoZhyhd> getZhyhdByNd(String nd) {
        try {
            List<GeoZhyhd> GeoZhyhds = geoZhyhdService.getZhyhdByNd(nd);
            return GeoZhyhds;
        } catch (Exception ee) {
            return null;
        }
    }

    @GetMapping("getZhyhdByNdAndKeywords")
    public List<GeoZhyhd> getZhyhdByNdAndKeywords(String nd, String keywords) {
        try {
            List<GeoZhyhd> GeoZhyhds = geoZhyhdService.getZhyhdByNdAndKeywords(nd,keywords);
            return GeoZhyhds;
        } catch (Exception ee) {
            return null;
        }
    }

    @GetMapping("delete")
    public boolean delete(Integer id) {
        try {
            deleteFile(id);
            return geoZhyhdService.deleteById(id);
        } catch (Exception ee) {
            return false;
        }
    }


    @GetMapping("getYhdYear")
    public List<String> getYhdYear() {
        try {

            return geoZhyhdService.getYhdYear();

        } catch (Exception ee) {
            return null;
        }
    }

    @RequestMapping(value = "/getFileByZhyhdid", method = RequestMethod.GET)
    @ResponseBody
    public List<TbUploadfile> getFileByZhyhdid(int id) {
        List<TbUploadfile> list = new ArrayList<>();
        try {
            List<TbYhdfilerelation> relations = tbYhdfilerelationService.getRelationsByZhyhdid(id);
            if (relations.size() > 0) {
                for (TbYhdfilerelation c : relations) {
                    TbUploadfile file = tbUploadfileController.selectOne(c.getFileid());
                    list.add(file);
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return list;

    }


}
